import filters from "@/store/js/filters"

export default {
	state: {
		token: null, //用户id获取
		userInfo: null, //用户信息
		dictInfo: null, //动态字典
	},
	getters: {
		getToken: state => {
			return state.token ? state.token : uni.getStorageSync("token");
		},
		getUserInfo: state => {
			return state.userInfo ? state.userInfo : uni.getStorageSync("userInfo");
		},
		getDictInfo: state => {
			return state.dictInfo ? state.dictInfo : uni.getStorageSync("dictInfo");
		},
	},
	mutations: {
		SET_TOKEN: (state, res) => {
			state.token = res
		},
		SET_USER_INFO: (state, res) => {
			state.userInfo = res
		},
		SET_DICT_INFO: (state, res) => {
			state.dictInfo = res
		}
	},
	actions: {
		getToken(context) {
			return new Promise((resolv, reject) => {
				filters.getToken().then(res => {
					context.commit("SET_TOKEN", res);
					// resolv(res.token);
					resolv(res);
					console.log('res', res);
				}).catch(error => {
					reject(error)
				})
			})
		},
		getUserInfo(context) {
			return new Promise((resolv, reject) => {
				filters.getUserInfo().then(res => {
					if (res == null) {
						uni.setStorageSync("userInfo", null)
					} else {
						context.commit("SET_USER_INFO", res);
					}
					resolv(res);
				}).catch(error => {
					reject(error)
				})
			})
		},
		getDictInfo(context) {
			return new Promise((resolv, reject) => {
				filters.getDictInfo().then(res => {
					if (res == null) {
						uni.setStorageSync("dictInfo", null)
					} else {
						context.commit("SET_DICT_INFO", res);
					}
					resolv(res);
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}